package com.lungle.warmdiary.app.base;

import android.content.Context;
import android.widget.Toast;

import com.lungle.warmdiary.app.AppContext;
import com.lungle.warmdiary.app.api.exception.ApiException;

import java.io.IOException;

import retrofit2.adapter.rxjava.HttpException;
import rx.Observer;

/**
 * Created by God on 2016/8/20.
 */
public class BaseObserver<T> implements Observer<T> {
    private Context mContext;

    public BaseObserver() {

    }

    public BaseObserver(Context context) {
        mContext = context;
    }

    @Override
    public void onCompleted() {

    }

    @Override
    public void onError(Throwable e) {
        if (e instanceof HttpException) {
            AppContext.showToast("服务咱不可用");
        } else if (e instanceof IOException) {
            AppContext.showToast("网络连接失败");
        } else if (e instanceof ApiException) {
            ApiException exception = (ApiException) e;
            if (exception.isTokenExpried()) {
            } else {
                Toast.makeText(mContext, e.getMessage(), Toast.LENGTH_SHORT).show();
            }
        }
    }

    @Override
    public void onNext(T t) {

    }
}
